package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *报价单
 *
 *@author weijianxun
 */
@Service("quotationDynamicStrategyImpl")
public class QuotationDynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy {

    private final Logger LOG = LoggerFactory.getLogger(QuotationDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(QuotationEnum.CUSTOMER_ID.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
        refTextToLogTypeMap.put(QuotationEnum.OPPORTUNITY_ID.getAttr(), ProjectLogEnum.OPPORTUNITY_REF.getSubType());
        refTextToLogTypeMap.put(QuotationEnum.CONTACT_ID.getAttr(), ProjectLogEnum.CONTACT_REF.getSubType());

    }

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.QUOTATION.getCode());
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
        try {
            SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
            PaasFormDataEntity newPaasFormDataEntity = saasFormSaveDTO.getNewPaasFormDataEntity();
            JSONObject data = newPaasFormDataEntity.getData();
            String version = data.getString(QuotationEnum.VERSION.getAttr());
            boolean isNotNewVersion = Objects.equals(version, StringConstant.DEFAULT_VERSION);
            Long quotationId = newPaasFormDataEntity.getId();
            String memo;
            if (Objects.equals(saasFormSaveDTO.getIsImport(), BasicConstant.ONE)) {
                memo = isNotNewVersion ? CrmLogMemoConstant.DYNAMIC_USER_IMPORT : CrmLogMemoConstant.DYNAMIC_USER_IMPORT_NEW_VERSION;
            } else {
                memo = isNotNewVersion ? CrmLogMemoConstant.DYNAMIC_USER_ADD : CrmLogMemoConstant.DYNAMIC_USER_ADD_NEW_VERSION;
            }
            PaasFormEntity paasForm = getPaasForm(saasFormSaveDTO.getCorpid(), newPaasFormDataEntity.getFormId(), saasFormSaveDTO.getBusinessType());
            saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, paasForm,
                    DynamicRefInfoDTO.initDynamicRefInfoDTO(quotationId, saasFormSaveDTO.getBusinessType(), newPaasFormDataEntity.getSerialNo()),
                    memo, null, ProjectLogEnum.QUOTATION_CREATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.QuotationDynamicStrategyImpl.save：", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, null, QuotationEnum.QUOTATION_NO.getAttr(), ProjectLogEnum.QUOTATION_DELETE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.QuotationDynamicStrategyImpl.batchDelete", e);
        }
    }
}
